package com.itany.boot.nmms.controller;

import com.itany.boot.nmms.entity.Staff;
import com.itany.boot.nmms.exception.*;
import com.itany.boot.nmms.service.StaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/staff")
public class StaffController {

    @Autowired
    private StaffService staffService;

    @RequestMapping("/login")
    public String login(HttpServletRequest request, HttpSession session, String loginName, String password, String role, String code) {
        try {
            Staff staff = staffService.checkUsernameAndPassword(loginName, password, code, role, session);
            session.setAttribute("staff", staff);
            return "redirect:/nmms/main";

        } catch (RequestParamsException e) {
            request.setAttribute("loginMsg", e.getMessage());
            return "backend/login";
        } catch (VerificationCodeException e) {
            request.setAttribute("loginMsg", e.getMessage());
            return "backend/login";
        } catch (StaffNotExistException e) {
            request.setAttribute("loginMsg", e.getMessage());
            return "backend/login";
        } catch (MD5Exception e) {
            request.setAttribute("loginMsg", e.getMessage());
            return "backend/login";
        } catch (SelectIdentityException e) {
            request.setAttribute("loginMsg", e.getMessage());
            return "backend/login";
        }catch (Exception e) {
            request.setAttribute("loginMsg", "服务器内部异常");
            return "backend/login";
        }


    }

    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "redirect:/nmms/login";
    }

    @RequestMapping("/show")
    public ModelAndView show() {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("backend/staffManager");
        return mav;
    }




}
